<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\enum\CommonEnum;
use app\model\AccountDailyStatModel;
use app\model\AccountPortModel;
use app\model\ClientAccountModel;
use app\service\client\AccountService;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use think\facade\Db;

class TestOeWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('test_oe');
        $this->setDescription('test_oe');
        //        $this->addArgument('media_id', Argument::REQUIRED, '媒体id');
        //        $this->addArgument('stat_date', Argument::REQUIRED, '日期');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $token = AccountService::getToken(CommonEnum::OE_MEDIA, '1773653488272392', 1);
        echo $token;die;
        $advertiserIds = ClientAccountModel::where('media_id', 1)->field('id,advertiser_id')->select();
        foreach ($advertiserIds as $v) {
            $res = Db::connect('mongodb')->table('habo_oe_video_day_report')
                ->where('account_id', intval($v['id']))->update(['is_archive' => 0]);
            if ($res) {
                echo $v['advertiser_id'] . '成功' . PHP_EOL;
            }
        }
        echo 'ok!';
        exit;

        $data = Db::connect('mongodb')->table('habo_oe_account_day_report')->field('_id,account_id,account_type,stat_date,adv_id')->select();
        foreach ($data as $v) {
            if (empty($v['account_type'])) {
                $accountType = AccountDailyStatModel::where('account_id', $v['account_id'])->where('stat_date', $v['stat_date'])->value('account_type');
                if (! $accountType) {
                    $accountType = ClientAccountModel::where('id', $v['account_id'])->value('account_type');
                }
                if ($accountType > 0) {
                    $res = Db::connect('mongodb')->table('habo_oe_account_day_report')->where(['_id' => $v['_id']])->update(['account_type' => intval($accountType)]);
                    if ($res) {
                        echo $v['adv_id'] . ' : ' . $v['stat_date'] . '成功' . PHP_EOL;
                    }
                }
            }
        }
        echo 'ok';
        exit;

        $data = Db::connect('mongodb')->table('habo_oe_account_day_report')->field('_id,stat_date')->select();
        foreach ($data as $v) {
            $statMonth = intval(date('Ym', strtotime(strval($v['stat_date']))));
            if ($statMonth) {
                $res = Db::connect('mongodb')->table('habo_oe_account_day_report')
                    ->where('_id', $v['_id'])->update(['stat_month' => $statMonth]);
                if ($res) {
                    echo $v['_id'] . ' : ' . $v['stat_date'] . '成功' . PHP_EOL;
                }
            }
        }
        echo 'ok';
        exit;

        $advertiserIds = ClientAccountModel::where('media_id', 1)->field('id,advertiser_id')->select();
        foreach ($advertiserIds as $v) {
            $productId = AccountPortModel::where('advertiser_id', $v['advertiser_id'])->value('product_id');
            if ($productId) {
                $res = Db::connect('mongodb')->table('habo_oe_account_day_report')
                    ->where('account_id', intval($v['id']))->update(['product_id' => intval($productId)]);
                $res1 = Db::connect('mongodb')->table('habo_oe_account_day_old_report')
                    ->where('account_id', intval($v['id']))->update(['product_id' => intval($productId)]);
                if ($res || $res1) {
                    echo $v['advertiser_id'] . '成功' . PHP_EOL;
                }
            }
        }
        echo 'ok!';
        exit;
        $data = Db::connect('mongodb')->table('habo_oe_account_day_old_report')->field('_id,account_id,stat_date,adv_id,account_type')->select();
        foreach ($data as $v) {
            if (! isset($v['account_type'])) {
                $accountType = ClientAccountModel::where('id', $v['account_id'])->value('account_type');
            } else {
                $accountType = AccountDailyStatModel::where('account_id', $v['account_id'])->where('stat_date', $v['stat_date'])->value('account_type');
            }
            if ($accountType > 0) {
                $res = Db::connect('mongodb')->table('habo_oe_account_day_old_report')->where(['_id' => $v['_id']])->update(['account_type' => intval($accountType)]);
                if ($res) {
                    echo $v['adv_id'] . ' : ' . $v['stat_date'] . '成功' . PHP_EOL;
                }
            }
        }
        echo 'ok';
        exit;

        $flag = true;
        while ($flag) {
            try {
                Db::connect('mongodb')->table('habo_oe_video_sync_log')->where(['stat_date' => 20230724])->delete();
            } catch (\Exception $e) {
                dump($e);
                echo date('H:i:s') . PHP_EOL;
                continue;
            }
            $flag = false;
        }
        echo 'ok';
        exit;

        $output->writeln('=======刷新token结束' . date('Y-m-d H:i:s', time()) . '=======');
        $output->writeln('success');
    }
}
